#ifndef I2C_SLAVE_H
#define I2C_SLAVE_H
#include <Arduino.h>
#include <Wire.h>
#include "CircularBuffer.h"
#include "mail.h"
// #define MODULE_REG_DATA_LEN 8

class I2CSlave 
{
public:
enum RxStatus {
    NoData         = 0,
    ReadAddressed  = 1,
    WriteGeneral   = 2,
    WriteAddressed = 3
};
    // struct _module_reg_address{
    //     union{
    //         uint8_t data[2];
    //         uint8_t value;
    //     };
    //     size_t len;
    // };
    I2CSlave(Mail<>& mb);
    ~I2CSlave();
    typedef void (*receive_callback)(int);
    typedef void (*request_callback)();
    void begin();
    void begin(int address);
    void frequency (uint32_t hz=100000);
    void on_receive(receive_callback cb);
    void on_request(request_callback cb);

    bool read(os::memory_pool_t::memory_node_t& data);


    int write (const uint8_t* data, int length);
    int write (int data);
    void stop (void);
    void end();
    
    uint8_t get_reg_read_address();
 

    void handle_on_receive(int howMany,uint8_t* large_data);
    void handle_header_on_receive(int howMany,uint8_t* reg_data);
private:
    Mail<>& _mb;
    int _address;

    uint8_t _reg_read_address;

    volatile bool _receive_updated;
    volatile bool _updated;

};

#endif